package marksys.free.com.marksys.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import marksys.free.com.marksys.R;

public class ArrayListAdapter extends RecyclerView.Adapter<ArrayListAdapter.SimpleHolder> {
    private ArrayList<String> mDatas;
    private Context mContext;
    private int mResID;

    public ArrayListAdapter(Context mContext, int mResID, ArrayList<String> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        this.mResID = mResID;
    }

    @NonNull
    @Override
    public SimpleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        TextView tv_name = new AppCompatCheckBox(parent.getContext());
        tv_name.setTextColor(parent.getResources().getColorStateList(R.color.color_filter));
        View view= LayoutInflater.from(mContext).inflate(mResID,parent,false);
        return new SimpleHolder(view);//加载item的布局
    }

    @Override
    public void onBindViewHolder(@NonNull SimpleHolder holder, int position) {
        //item的单击事件
        holder.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                int layoutPosition = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(holder.itemView, layoutPosition);
            }
        });
        //设置item的子view数据
        holder.tv_name.setText( mDatas.get(position));
    }

    @Override
    public int getItemCount() {
        return mDatas==null?0:mDatas.size();
    }

    class SimpleHolder extends RecyclerView.ViewHolder {
        private TextView tv_name;
        SimpleHolder(View itemView) {
            super(itemView);
            //初始化id
            tv_name= itemView.findViewById(R.id.tv_name);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

}
